<%--
  Created by IntelliJ IDEA.
  User: 诸葛龙辰
  Date: 2023/3/3
  Time: 20:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>更改</title>
    <script type="text/javascript" src="/js/jquery-3.4.1.min.js"></script>
<%--    <script>--%>
<%--        let empId = ${param.empId};--%>
<%--        alert("修改的id为："+empId);--%>
<%--    </script>--%>
</head>
<body>
<h2 align="center">更改</h2>
<div id="error" ></div>
<form id="empFrm">
    <table border="1" width="600px" align="center">
        <tr style="background-color: red">
            <td>员工编号：</td>
            <td><input  readonly="readonly"  type="hidden" name="empId" id="empId"></td>
        </tr>
        <tr style="background-color: red">
            <td>员工名称：</td>
            <td><input type="text" name="empName" id="empName"></td>
        </tr>
        <tr style="background-color: red">
            <td>电话号码：</td>
            <td><input type="text" name="phoneNum" id="phoneNum" ></td>
        </tr>
        <tr style="background-color: red">
            <td>部门：</td>
            <td><input type="text" name="deptNo" id="deptNo"></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="button" id="btn" value="更新"></td>
        </tr>
    </table>
</form>
<script>
    let empId = ${param.empId};
    $( function (){
        $.ajax({
            url:"/EmpServlet/queryById",
            type:"get",//请求的方法
            data:{empId:empId},
            contentType:"json",//定义返回值发格式
            success:function (data) {//成功回调函数 data=返回的json 对象

                $("#empId").val(data.empId);
                $("#empName").val(data.empName);
                $("#phoneNum").val(data.phoneNum);
                $("#deptNo").val(data.deptNo);
            },
            error:function (data){
            }
        });
        $("#btn").click(function (){
            var  emp = $("#empFrm").serialize();
            $.ajax({
                url:"/EmpServlet/update",
                type:"get",//请求的方法
                data:emp,
                contentType:"json",
                success:function (data){
                    if (data>0){
                     location.href="/emp/list.jsp";
                    }else {
                        $("#error").html("添加失败！")
                    }
                },
                error:function (data){

                }
            });
        })
    });
</script>
</body>
</html>
